﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Data.Entity;
using System.Windows.Forms;
using System.Data.Objects;
using ThaiHoa.Win.QLSX.Entity;
using ThaiHoa.Win.QLSX.QuanLyKho.UTI;
using Excel = Microsoft.Office.Interop.Excel;

namespace ThaiHoa.Win.QLSX.QuanLyKho.UI
{
    public partial class frmMacVatTu : Form
    {
        #region Fields
        QuanLyKhoDBContext db;
        BindingSource loaiVatTuBS;
        BindingSource macVatTuBS;
        private bool HasChanged = false;
        #endregion
        #region Contructor
        public frmMacVatTu()
        {
            InitializeComponent();
        }
        #endregion
        #region Functions
        private void BindData()
        {
            db = new QuanLyKhoDBContext();
            macVatTuBS = new BindingSource();
            macVatTuBS.AddingNew += new AddingNewEventHandler(thepDacBS_AddingNew);
            macVatTuBS.ListChanged += new ListChangedEventHandler(thepDacBS_ListChanged);
            db.MacVatTu.Where(m => m.TrangThai).Load();
            macVatTuBS.DataSource = db.MacVatTu.Local.ToBindingList();
            loaiVatTuBS = new BindingSource();
            db.LoaiVatTu.Where(l => l.TrangThai).Load();
            loaiVatTuBS.DataSource = db.LoaiVatTu.Local.ToBindingList();
            //binding for navigator
            macVatTuBN.BindingSource = macVatTuBS;
            //binding for gridview
            dgvMacVatTu.AutoGenerateColumns = true;
            dgvMacVatTu.DataSource = macVatTuBS;
            CustomerGridView();
            //binding for textbox
            txtTenMac.DataBindings.Add("Text", macVatTuBS, "TenMac");
            //binding for combobox
            cbLoaiVatTu.DataBindings.Add("SelectedItem", macVatTuBS, "LoaiVatTu", true);
            cbLoaiVatTu.DataSource = loaiVatTuBS;
        }
        private void CustomerGridView()
        {
            //Ẩn toàn bộ columns
            foreach (DataGridViewColumn item in dgvMacVatTu.Columns)
            {
                item.Visible = false;
            }
            //Tùy chỉnh lại

            //Tên
            var c = dgvMacVatTu.Columns["TenMac"];
            c.Visible = true;
            c.Width = 200;
            c.DisplayIndex = 1;

            //Loại vật tư
            c = dgvMacVatTu.Columns["LoaiVatTu"];
            c.Visible = true;
            c.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            c.ReadOnly = true;
            c.DisplayIndex = 2;

        }
        private void SaveChange()
        {
            if (HasChanged)
            {
                if (MessageBox.Show("Lưu thay đổi ?", "", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    db.SaveChanges();
                }
            }
        }

        private void ExportToExcel()
        {
            string workbookPath = @"c:\JAT_VatTu_MacVatTu";
            Excel.Application xlApp = new Excel.Application();
            Excel.Workbook xlWorkBook = xlApp.Workbooks.Open(workbookPath,
                0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "",
                true, false, 0, true, false, false);
            Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets[1];
            xlWorkBook.Activate();
            xlWorkSheet.Name = "MacVatTu";
            //Add data
            var listMacVatTu = macVatTuBS.List;
            int count = 0;
            Excel.Range r;
            foreach (MacVatTu item in listMacVatTu)
            {
                xlWorkSheet.Cells[8 + count, 1] = count + 1;
                xlWorkSheet.Cells[8 + count, 2] = item.Tenmac;
                xlWorkSheet.Cells[8 + count, 3] = item.LoaiVatTu.TenLoai;

                //Format it               

                string start = "A" + (count + 8).ToString();
                string stop = "C" + (count + 8).ToString();
                r = (Excel.Range)xlWorkSheet.get_Range(start, stop);
                r.Borders.Color = Color.Black;
                r.RowHeight = 30;
                //r.Style.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
                r.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThin, Excel.XlColorIndex.xlColorIndexAutomatic, Excel.XlColorIndex.xlColorIndexAutomatic);
                count++;

            }
            // Ẩn chương trình
            xlApp.Visible = false;

            // Save file
            xlWorkBook.SaveAs("C:\\JAT_VatTu_MacVatTu_1.xls", Excel.XlFileFormat.xlWorkbookNormal,
                            null, null, false, false,
                            Excel.XlSaveAsAccessMode.xlExclusive,
                            false, false, false, false, false);
            xlApp.Quit();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkBook);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);



            MessageBox.Show("File created !");
        }
        #endregion
        #region Events
        private void frmMacVatTu_Load(object sender, EventArgs e)
        {

            BindData();
            HasChanged = false;
        }
        private void saveToolStripButton_Click(object sender, EventArgs e)
        {
            SaveChange();
        }
        private void dgvMacVatTu_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            Common.ShowMessengerError(e.Exception.Message);
        }

        private void dgvMacVatTu_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            //

        }

        private void dgvMacVatTu_CellValidated(object sender, DataGridViewCellEventArgs e)
        {
           

        }


        private void thepDacBS_AddingNew(object sender, AddingNewEventArgs e)
        {

            MacVatTu macVatTu = new MacVatTu();
            LoaiVatTu loaiVatTu = db.LoaiVatTu.FirstOrDefault();
            if (loaiVatTu == null)
            {
                loaiVatTu = new LoaiVatTu();
                loaiVatTu.TenLoai = "Thép Đặc";

            }
            macVatTu.LoaiVatTu = loaiVatTu;
            e.NewObject = macVatTu;

        }
        private void thepDacBS_ListChanged(object sender, ListChangedEventArgs e)
        {
            HasChanged = true;
        }
        private void printToolStripButton_Click(object sender, EventArgs e)
        {

        }

        private void exportToolTripButton_Click(object sender, EventArgs e)
        {
            ExportToExcel();
        }
        private void frmMacVatTu_FormClosing(object sender, FormClosingEventArgs e)
        {
            SaveChange();
        }

        #endregion

      

    }
}
